const {con} = require('../db/db');

async function postWork(title,userid,content,images,create_time) {
    const sql = `insert into works(title,userid,content,images,create_time) values("${title}",${userid},"${content}","${images}","${create_time}");`;

    await con.execute(sql);
}

async function getTextrueList(page) {
    const sql = `select * from textrue order by id limit ${(page-1)*10},10;`;

    const total = `select * from textrue;`;

    let [rows] = await con.execute(sql);

    let [totals] = await con.execute(total);

    return {data:rows,total:totals.length}
}

async function getModelImgsList(page) {
    const sql = `select * from modelimg order by id limit ${(page-1)*10},10;`;

    const total = `select * from modelimg;`;

    let [rows] = await con.execute(sql);

    let [totals] = await con.execute(total);

    return {data:rows,total:totals.length}
}

async function delImgById(id) {
    const selsql = `select image from images where id=${id};`,
    sql = `delete from images where id=${id};`;

    let res = '';

    res = await con.execute(selsql);

    await con.execute(sql);

    return res;
}

async function getCourseList(page) {
    const sql = `select * from course order by id limit ${(page-1)*10},10;`;

    const total = `select * from course;`;

    let [rows] = await con.execute(sql);

    let [totals] = await con.execute(total);

    return {data:rows,total:totals.length}
}

async function postAvatar(userid,avatar) {
    const sql_update = `update user set avatar='${avatar}' where id=${userid};`;

    await con.execute(sql_update);

    const sql_select = `select * from user where id=${userid};`;

    let [rows] = await con.execute(sql_select);

    return {code:200,data:rows[0]}
}

module.exports={
    postWork,
    getTextrueList,
    getModelImgsList,
    delImgById,
    getCourseList,
    postAvatar
}